# -*- coding: utf-8 -*-
"""
Created on Fri Dec 17 20:53:06 2021

@author: wulong
"""
import numpy as np
from casadi import *
from Basic_paras import *

# In[1] Slow MPC model
# States
def ode_ies_s(xs, xm, xf, us, um, uf, uz, ud):
    dxdt = SX.zeros(Nx_s)
    
    SOC = xs[0]
    mstc = xs[1]
    mt_stc = xs[2]
    mt_sth = xs[3]
    tbr = xs[4]
    
    pH2O = xm[0]
    pH2 = xm[1]
    Pmtf = xm[2]
    tabf = xm[3]
    tabw = xm[4]
    tabt = xm[5]
    tcwm = xm[6]
    tewm = xm[7]
    tre = xm[8]
    
    If = xf[0]
    Gh2 = xf[1]
    pO2 = xf[2]
    tc = xf[3]
    tcs = xf[4]
    te = xf[5]
    tes = xf[6]
    vcap = xf[7]
    Iba = xf[8]
    
    Gab = us[0]
    Gec = us[1]
    Gstu = us[2]
    
    Gfm = um[0]
    
    Gff = uf[0]
    Nec = uf[1]
    
    zfc = uz[0]
    zma = uz[1]
    zec = uz[2]
    zst = uz[3]
    
    ta = ud[0]
    ins = ud[1]
    Pd = ud[2]
    Qother = ud[3]
    
    # real ST input with binary
    Gst = zst*Gstu + (zst - 1)*Gstu
    
    # ST in the return side
    msth = Mst - mstc
    tsth = mt_sth/(msth + eps)
    thp = zst*tre + (1 - zst)*tsth
    
    # PIP in the rerurn side
    Gsl = Gab + Gec + Gst
    trec = (Gsl*tre - Gst*thp)/(Gab + Gec + eps)
    
    # MA
    tab = zma*(tab0 + tabf + tabw + tabt)
    
    # EC
    tec = zec*(2*tewm - trec)
    
    # PIP in the supply side 1
    tslc = (Gab*tab + Gec*tec)/(Gab + Gec + eps)
    
    # ST in the supply side
    tstc = mt_stc/(mstc + eps)
    tcp = zst*(tstc) + (1 - zst)*tslc
    
    # PIP in the supply side 2
    tsl = (Gab*tab + Gec*tec + Gst*tcp)/(Gsl + eps)
    
    # BR
    Qsl = Gsl*Cw*(tre - tsl)
    
    # BA
    iba = Iba/npb
    
    dxdt[0] = -iba/(3600*Ceb)
    dxdt[1] = -Gst
    dxdt[2] = -Gst*tcp
    dxdt[3] = Gst*thp
    dxdt[4] = (Ubr*(ta - tbr) - Qsl + Qother)/Cbr
    
    return dxdt

# Constraints on medium & fast states
def const_ies_s(xs, xm, xf, us, um, uf, uz, ud):
    x_mf_const = MX.zeros(Nx_m + Nx_f)
    
    SOC = xs[0]
    mstc = xs[1]
    mt_stc = xs[2]
    mt_sth = xs[3]
    tbr = xs[4]
    
    pH2O = xm[0]
    pH2 = xm[1]
    Pmtf = xm[2]
    tabf = xm[3]
    tabw = xm[4]
    tabt = xm[5]
    tcwm = xm[6]
    tewm = xm[7]
    tre = xm[8]
    
    If = xf[0]
    Gh2 = xf[1]
    pO2 = xf[2]
    tc = xf[3]
    tcs = xf[4]
    te = xf[5]
    tes = xf[6]
    vcap = xf[7]
    Iba = xf[8]
    
    Gab = us[0]
    Gec = us[1]
    Gstu = us[2]
    
    Gfm = um[0]
    
    Gff = uf[0]
    Nec = uf[1]
    
    zfc = uz[0]
    zma = uz[1]
    zec = uz[2]
    zst = uz[3]
    
    ta = ud[0]
    ins = ud[1]
    Pd = ud[2]
    Qother = ud[3]
    
    # real ST input with binary
    Gst = zst*Gstu + (zst - 1)*Gstu
    
    # PV
    I_pv_max = I_max_0*ins/Ins_pv_0*(1 + c_pv1*(ta - T_pv_0))
    V_pv_max = V_max_0*log(exp(1) + c_pv2*(ins - Ins_pv_0))*(1 - c_pv3*(ta - T_pv_0))
    Ppv = npp*nsp*I_pv_max*V_pv_max/1000
    
    # FC
    eta_a = afc + bfc*log(If + eps)
    eta_c = -R0fc*T0fc/(2*F0)*log(1-If/IL + eps)
    eta_o = If*rfc
    V0 = N0fc*(E0fc + R0fc*T0fc/(2*F0)*log(pH2*sqrt(pO2)/(pH2O + eps) + eps))
    Vfc = V0 - eta_a - eta_c - eta_o
    Ifc = fu_d/(2*Kr)*Gh2
    Pfc = zfc*(Vfc*Ifc/1000)
    Gff_mol = Gff*Mch4
    Go2 = 1/r_HO*Gh2
    Gh2r = 2*Kr*If
    
    # ST in the return side
    msth = Mst - mstc
    tsth = mt_sth/(msth + eps)
    thp = zst*tre + (1 - zst)*tsth
    
    # PIP in the rerurn side
    Gall = Gab + Gec + Gstu
    Gsl = Gab + Gec + Gst
    trec = (Gsl*tre - Gst*thp)/(Gab + Gec + eps)
    
    # MA
    Pmt = zma*(Pmt0 + Pmtf)
    tab = zma*(tab0 + tabf + tabw + tabt)
    
    # EC
    pc = exp(21.3-2025.5/(248.94+tc))/1e6
    pe = exp(21.3-2025.5/(248.94+te))/1e6
    hcro = -137.26+1.23463*(273.15+tc) - hspc
    hero = 338.02893+0.24532*(273.15+te) + hsph
    tcwo = 2*tcwm - tcwi
    tec = zec*(2*tewm - trec)
    yb = pc/pe # Compressor
    etavl = 0.98-0.085*(yb**(1/kr)-1)
    etacp = 0.9085*exp(-0.06443*yb)-7.605*exp(-3.155*yb)
    Gr = etavl*Nec*roeg*Vcp
    wi = kr/(1e3*(kr-1))*pe*1e6/roeg*(yb**((kr-1)/kr)-1)
    hcri = hero + wi
    heri = hcro # Expansion valve
    Pcp = zec*(Gr*wi/etacp)
    
    # PIP in the supply side 1
    tslc = (Gab*tab + Gec*tec)/(Gab + Gec + eps)
    
    # ST in the supply side
    tstc = mt_stc/(mstc + eps)
    tcp = zst*(tstc) + (1 - zst)*tslc
    
    # PIP in the supply side 2
    tsl = (Gab*tab + Gec*tec + Gst*tcp)/(Gsl + eps)
    
    # BR
    Q_the = Qsl0*(tbr - tsl)/delta_twa0*(Gsl/Gsl0)**0.6
    tre_cal = tsl + Q_the/(Gsl*Cw)
    Hpmp = Spmp*Gall**2/(rhow*ge)
    eta_pmp = b0p*Gall**2 + b1p*Gall + b2p
    Ppmp = Gall*ge*Hpmp/(1e3*eta_pmp)
    
    # BA
    iba = Iba/npb
    Vba = nsb*(Em - vcap - R0b*iba)
    Psc = Pcp + Ppmp
    Pdb = Pd + Psc - Ppv - Pfc - Pmt
    Iba_cal = Pdb*1000/Vba
        
    x_mf_const[0] = zfc*((Ifc - If)/tau_e)
    x_mf_const[1] = zfc*((Gff_mol - Gh2)/tau_f)
    x_mf_const[2] = zfc*((1/KO2*(Go2 - If*Kr) - pO2)/tau_O2)
    x_mf_const[3] = zfc*((1/KH2O*Gh2r - pH2O)/tau_H2O)
    x_mf_const[4] = zfc*((1/KH2*(Gh2- Gh2r) - pH2)/tau_H2)
    x_mf_const[5] = zma*((kma1*Gfm**2 + kma2*Gfm + kma3 - Pmtf)/tau_mtf)
    x_mf_const[6] = zma*((kma4*Pmtf - tabf)/tau_abf)
    x_mf_const[7] = zma*((kma5*Gab + kma6 - tabw)/tau_abw)
    x_mf_const[8] = zma*((kma7*trec + kma8 - tabt)/tau_abt)
    x_mf_const[9] = zec*((Gr*(hcri - hcro) + alfar*Aci*(tcs - tc))/(Ccr*Mcr))
    x_mf_const[10] = zec*((alfar*Aci*(tc - tcs) + alfaw*Aco*(tcwm - tcs))/(Cs*Mcs))
    x_mf_const[11] = zec*((Cw*Gcw*(tcwi - tcwo) + alfaw*Aco*(tcs - tcwm))/(Cw*Mcw))
    x_mf_const[12] = zec*((Gr*(heri - hero) + alfar*Aei*(tes - te))/(Cer*Mer))
    x_mf_const[13] = zec*((alfar*Aei*(te - tes) + alfaw*Aeo*(tewm - tes))/(Cs*Mes))
    x_mf_const[14] = zec*((Cw*Gec*(trec - tec) + alfaw*Aeo*(tes - tewm))/(Cw*Mew))
    x_mf_const[15] = iba/C1b - vcap/(R1b*C1b)
    x_mf_const[16] = (Iba_cal - Iba)/tau_dc
    x_mf_const[17] = (tre_cal - tre)/tau_ahu
    
    return x_mf_const

# Output
def out_ies_s(xs, xm, xf, us, um, uf, uz, ud):
    y_alg = MX.zeros(Ny_s)
    
    SOC = xs[0]
    mstc = xs[1]
    mt_stc = xs[2]
    mt_sth = xs[3]
    tbr = xs[4]
    
    pH2O = xm[0]
    pH2 = xm[1]
    Pmtf = xm[2]
    tabf = xm[3]
    tabw = xm[4]
    tabt = xm[5]
    tcwm = xm[6]
    tewm = xm[7]
    tre = xm[8]
    
    If = xf[0]
    Gh2 = xf[1]
    pO2 = xf[2]
    tc = xf[3]
    tcs = xf[4]
    te = xf[5]
    tes = xf[6]
    vcap = xf[7]
    Iba = xf[8]
    
    Gab = us[0]
    Gec = us[1]
    Gstu = us[2]
    
    Gfm = um[0]
    
    Gff = uf[0]
    Nec = uf[1]
    
    zfc = uz[0]
    zma = uz[1]
    zec = uz[2]
    zst = uz[3]
    
    ta = ud[0]
    ins = ud[1]
    Pd = ud[2]
    Qother = ud[3]
        
    # PV
    I_pv_max = I_max_0*ins/Ins_pv_0*(1 + c_pv1*(ta - T_pv_0))
    V_pv_max = V_max_0*log(exp(1) + c_pv2*(ins - Ins_pv_0))*(1 - c_pv3*(ta - T_pv_0))
    Ppv = npp*nsp*I_pv_max*V_pv_max/1000
        
    # FC
    eta_a = afc + bfc*log(If + eps)
    eta_c = -R0fc*T0fc/(2*F0)*log(1-If/IL + eps)
    eta_o = If*rfc
    V0 = N0fc*(E0fc + R0fc*T0fc/(2*F0)*log(pH2*sqrt(pO2)/(pH2O + eps) + eps))
    Vfc = V0 - eta_a - eta_c - eta_o
    Ifc = fu_d/(2*Kr)*Gh2
    Pfc = zfc*(Vfc*Ifc/1000)
    
    # PIP in the rerurn side
    Gall = Gab + Gec + Gstu
        
    # MA
    Pmt = zma*(Pmt0 + Pmtf)
    
    # EC
    pc = exp(21.3-2025.5/(248.94+tc))/1e6
    pe = exp(21.3-2025.5/(248.94+te))/1e6
    yb = pc/pe # Compressor
    etavl = 0.98-0.085*(yb**(1/kr)-1)
    etacp = 0.9085*exp(-0.06443*yb)-7.605*exp(-3.155*yb)
    Gr = etavl*Nec*roeg*Vcp
    wi = kr/(1e3*(kr-1))*pe*1e6/roeg*(yb**((kr-1)/kr)-1)
    Pcp = zec*(Gr*wi/etacp)
    
    # BR
    Hpmp = Spmp*Gall**2/(rhow*ge)
    eta_pmp = b0p*Gall**2 + b1p*Gall + b2p
    Ppmp = Gall*ge*Hpmp/(1e3*eta_pmp)
    
    # BA
    iba = Iba/npb
    Vba = nsb*(Em - vcap - R0b*iba)
    Pba = Vba*Iba/1000
    
    # Electric Power
    Psc = Pcp + Ppmp
    Psl = Ppv + Pfc + Pmt + Pba - Psc
    
    y_alg[0] = Psl
    y_alg[1] = tbr
        
    return y_alg

# In[2] Formulate discrete time dynamics {xs, xm, xf, us, um, uf, uz, ud}
x_sym_s = SX.sym('x_s', Nx_s)
x_m_sym_s = SX.sym('x_m_s', Nx_m)
x_f_sym_s = SX.sym('x_f_s', Nx_f)

u_sym_s = SX.sym('u_s', Nuc_s)
u_m_sym_s = SX.sym('u_m_s', Nuc_m)
u_f_sym_s = SX.sym('u_f_s', Nuc_f)

uz_sym_s = SX.sym('uz_s', Nuz_l)
ud_sym_s = SX.sym('ud_s', Nud_s)

ode_sym_s = ode_ies_s(x_sym_s, x_m_sym_s, x_f_sym_s, u_sym_s, u_m_sym_s, u_f_sym_s, 
                      uz_sym_s, ud_sym_s)
args_s = {'x': x_sym_s, 'p': vertcat(x_m_sym_s, x_f_sym_s, u_sym_s, u_m_sym_s, u_f_sym_s, 
                                     uz_sym_s, ud_sym_s), 'ode': ode_sym_s}
opts_s = {'tf': Delta_s, 'regularity_check': True}
I_ode_s = integrator('I_ode_s', 'rk', args_s, opts_s)

